﻿using Sunxsh.Es.Common.Util;
using Sunxsh.Es.Web.Core.Filters;
using Microsoft.AspNetCore.Mvc.Formatters;

namespace Sunxsh.Es.Web.Core.Extensions;

public static class ControllerEx
{
    public static IMvcBuilder AddHuaskyController(this IServiceCollection services)
    {
        var builder = services.AddControllers();

        //控制器设置
        builder.AddMvcOptions(option =>
        {
            //默认的StringOutputFormatter会将字符串转换为简单类型输出，导致数字类型的字符串可能被前端识别为数字
            option.OutputFormatters.RemoveType<StringOutputFormatter>();
            option.Filters.Add<GlobalExceptionFilter>();
        });

        builder.AddNewtonsoftJson(option =>
        {
            option.SerializerSettings.ContractResolver = UtilEx.JsonSetting.ContractResolver;
            option.SerializerSettings.ReferenceLoopHandling = UtilEx.JsonSetting.ReferenceLoopHandling;
            option.SerializerSettings.DateFormatString = UtilEx.JsonSetting.DateFormatString;
        });

        return builder;
    }
}
